define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config) {
            // 流程
            $scope.phases = [
                'process_panel_one',
                'process_panel_two',
                'process_pcs_no'
            ];

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                $scope.double_panel_btn = false; // 雙面板按鈕切換
                $scope.pcs_list = { 
                    lot_no: '',
                    area_no: '',
                    op_no: '',
                    log_group_serial: '',
                    panel_one: '',
                    panel_two: ''
                };
                $scope.pcsNo_list = []; // 產品序號資料
                $scope.phases_index = 0; // 預設流程狀態
                $scope.double_panel_status = 0; // 是否有勾選雙面板按鈕 → 0:預設 ; 1未勾選(只刷入板號1) ; 2有勾選(有刷入板號2)
            
                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            };

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    $scope.phases_index = index;
                    if($scope.phases_index == 0)
                    //回 板號1流程
                    {
                        $scope.pcs_list = {};
                        $scope.pcsNo_list = [];
                        $scope.double_panel_status = 0;
                    } 
                    else if($scope.phases_index == 1)
                    {
                        //回 板號2流程
                        if($scope.double_panel_status == 1)
                        {
                            $scope.phases_index = 2;
                            // 在板號1流程未勾選雙面板，無法回板號2流程，請回板號1流程重新進行調整
                            $scope.$root.showAlert($filter('translate')('SMT15.msg.column01'));
                        } 
                        else 
                        {
                            $scope.pcs_list.panel_two = '';
                        }; 
                    }
                }
            };

            // 是否開啟雙面板
            $scope.isDoublePanel = function(){
                // 0:預設 ; 1未勾選(只刷入板號1) ; 2有勾選(有刷入板號2)
                if($scope.double_panel_status == 1)
                    $scope.double_panel_btn = false;
                else if($scope.double_panel_status == 2)
                    $scope.double_panel_btn = true;
                else
                    $scope.double_panel_btn = !$scope.double_panel_btn;
            };

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    // 板號1流程
                    if($scope.phases_index == 0){
                        panel_one_info_get(barcode);
                    // 板號2流程
                    } else if($scope.phases_index == 1) {
                        if($scope.panel_one == barcode)
                            // 刷入的板號不可以跟板號1相同，請重新確認！
                            $scope.$root.showAlert($filter('translate')('SMT15.msg.column02'));
                        else
                            panel_two_info_get(barcode);
                    // 序號流程
                    } else if($scope.phases_index == 2){
                        var content = {
                            lot_no: $scope.pcs_list.lot_no,
                            pcs_no: barcode
                        };
                        pcs_no_check_process(content, function(data){
                            var isRepeat = $filter('filter')($scope.pcsNo_list, {pcs_no: data.pcs_no}, true);
                            // 刷入的序號重複或已綁定，請重新確認！
                            if (isRepeat && isRepeat.length > 0)
                            { return $scope.$root.showAlert($filter('translate')('SMT15.msg.column03')); }
                            else {
                                if (data.pcs_no) {
                                    $scope.pcsNo_list.unshift({
                                        pcs_no: data.pcs_no,
                                        status: 'N',
                                    });
                                } else {
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                };
                            }
                            
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    };
                };
            };

            // 刪除 產品序號
            $scope.removeRow = function (item) {
                $scope.pcsNo_list.splice($scope.pcsNo_list.indexOf(item), 1);
            };

            // 點擊確認按鈕送回資料
            $scope.confirm = function(){
                if($filter('filter')($scope.pcsNo_list, {status:'N'}, true).length > 0 ){
                    var content = {
                        lot_no: $scope.pcs_list.lot_no,
                        area_no: $scope.pcs_list.area_no,
                        op_no: $scope.pcs_list.op_no,
                        log_group_serial: $scope.pcs_list.log_group_serial,
                        panel_one: $scope.pcs_list.panel_one,
                        panel_two: $scope.pcs_list.panel_two,
                        pcsNo_list: $filter('filter')($scope.pcsNo_list, {status:'N'}, true)
                    };
                    if($scope.phases_index == 2){
                        pcs_no_set_process(content, function(){
                            $scope.$root.showToast($filter('translate')('SMT15.msg.success'));
                            $filter('filter')($scope.pcsNo_list, {status:'N'}, true).forEach(function(item){
                                item.status='Y';
                            });
                        });
                    }
                    else
                        $scope.$root.showAlert($filter('translate')('SMT15.msg.column04'));
                }else{
                    $scope.$root.showAlert($filter('translate')('SMT15.msg.column05'));
                }
                
            };

            // 取得 板號(1)資料
            function panel_one_info_get(panel_one){
                var content = {
                    panel_one: panel_one
                };
                panel_no_info_get(content, function(panel_data){
    
                    if(panel_data.panel_one){
                        panel_binding_check_process(panel_data.panel_one,function(data){
                            if(!data.isCheck)
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            else
                            {
                                $scope.pcs_list = panel_data;
                                // 0: 未綁定
                                if(data.isCheck == '0'){
                                    if(!$scope.double_panel_btn)
                                    {
                                        $scope.phases_index = 2;
                                        $scope.double_panel_status = 1;
                                    }   
                                    else
                                    {
                                        $scope.phases_index = 1;
                                        $scope.double_panel_status = 2;
                                    }   
                                }
                                // 1: 已綁定，只有單板號
                                else if(data.isCheck == '1')
                                {
                                    $scope.phases_index = 2;
                                    $scope.double_panel_status = 1;
                                    panel_pcs_no_list_info_get();
                                }
                                // 2: 已綁定，有雙板號
                                else
                                {
                                    $scope.phases_index = 2;
                                    $scope.double_panel_status = 2;
                                    $scope.pcs_list.panel_two = panel_data.panel_two;
                                    panel_pcs_no_list_info_get();
                                };
                            };
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    };
                });
            };
            
            // 取得 版號1 或 版號2 資料
            function panel_no_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.panel_no_info_get',
                    content : content,
                    success : function(data){
                        if(callback){
                            callback(data);
                        }    
                    }
                });
            };

            // 檢查 板號(1) 或 版號(2) 是否綁定
            function panel_binding_check_process(panel_no,callback){
                var content = {
                    panel_no: panel_no,
                };
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.panel_binding_check_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 取回 板號已綁定的序號資料
            function panel_pcs_no_list_info_get(){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.panel_pcs_no_list_info_get',
                    content : {
                        panel_one: $scope.pcs_list.panel_one,
                        panel_two: $scope.pcs_list.panel_two
                    },
                    success : function(data){
                        $scope.pcsNo_list = data.pcsNo_list;
                        if(data && data.pcsNo_list.length > 0)
                            $scope.pcsNo_list = data.pcsNo_list;
                        else
                            $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            };

            // 取得 板號(2)資料
            function panel_two_info_get(panel_two){
                var content = {
                    panel_one: $scope.pcs_list.panel_one,
                    panel_two: panel_two,
                    lot_no: $scope.pcs_list.lot_no,
                    op_no: $scope.pcs_list.op_no
                };
                panel_no_info_get(content, function(panel_data){
                    if(!panel_data.panel_two)
                    {
                        panel_binding_check_process(function(data){
                            if(!data.isCheck)
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            else
                                $scope.pcs_list.panel_two = panel_data.panel_two;
                                // 0: 未綁定
                                if(data.isCheck == '0')
                                    $scope.phases_index = 2;
                                else
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                        });
                    }
                    else
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                })
            };
            
            // 取回 序號資料
            function pcs_no_check_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.pcs_no_check_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 送回 序號資料
            function pcs_no_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.pcs_no_set_process',
                    content : content,
                    success : function(data){
                        // 20200607 modify by Oliver清空回到版號一
                        $scope.pcsNo_list=[];
                        $scope.phases_index=0;
                        $scope.pcs_list = { 
                            lot_no: '',
                            area_no: '',
                            op_no: '',
                            log_group_serial: '',
                            panel_one: '',
                            panel_two: ''
                        };
                        if(callback)
                            callback(data);
                    }
                });
            };
            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
